<template>
  <div class="flex direction-column height-100">
    <div class="button-group">
      <el-button type="primary" @click="add">新增</el-button>
    </div>
    <el-table
      v-loading="loading" class="table"
      :data="lists" height="100%"
      :header-cell-style="{
        backgroundColor: themeColor,
        color: 'white'
      }"
    >
      <div slot="empty" class="empty-lists">
        <svg-icon name="search"></svg-icon>
        <span class="empty-text">暂无数据</span>
      </div>
      <el-table-column v-for="column in columns" :key="column.prop" v-bind="column">
        <template v-if="column.prop === 'centralEnterprises'" #default="{ row }">
          <span>{{row.centralEnterprises === '0' ? '是' : '否'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" prop="id" align="center" width="160">
        <template #default="{ row }">
          <td-button icon="editor" title="编辑" @click="edit(row)"></td-button>
          <td-button
            icon="ashbin" title="删除"
            icon-style="color: var(--danger-color);border-color: var(--danger-color);"
            @click="del(row)"
          ></td-button>
        </template>
      </el-table-column>
    </el-table>

    <operate-modal ref="modal" :modal-title="modalTitle" :modal-data="modalData" @save-success="reload"></operate-modal>
  </div>
</template>

<script>
import OperateModal from '@/views/system-settings/enterprise-management/operate-modal'
import TdButton from '@/components/td-button'
import { confirmDel } from '@/utils'
import { getListEnterprise, del } from '@/api/enterprose'

const api = {
  getListEnterprise,
  del,
}

export default {
  name: 'enterprise-management',
  components: { OperateModal, TdButton },
  data () {
    return {
      lists: [],
      loading: false,
      modalTitle: '',
      modalData: {},
      columns: [
        { label: '单位名称', prop: 'enterpriseName', width: 300 },
        // { label: '社会信用代码', prop: 'socialCreditCode', },
        // { label: '是否央企', prop: 'centralEnterprises', },
        // { label: '法人代码', prop: 'corporateCode', },
        { label: '所属地区', prop: 'districtBelongName', },
        { label: '单位联系电话', prop: 'unitContactNumber', },
        { label: '所属行业', prop: 'belongIndustryName', },
        // { label: '所属领域', prop: 'belongField', },
        // { label: '是否能源加工转企业', prop: 'transferEnterprise', },
        { label: '能耗水平', prop: 'energyConsumptionLevelName', },
        { label: '经纬度', prop: 'lnglat', },
        // { label: '经度', prop: 'longitude', },
        // { label: '纬度', prop: 'latitude', },
        // { label: '单位类型', prop: 'unitTypeName', },
        // { label: '法人代表', prop: 'legalRepresentative', },
        // { label: '单位地址', prop: 'address', },
        // { label: '邮政编码', prop: 'zipCode', },
        // { label: '企业网址', prop: 'enterpriseUrl', },
        // { label: '电子邮箱', prop: 'email', },
        // { label: '传真', prop: 'fax', },
        // { label: '注册日期', prop: 'registrationDate', },
        // { label: '注册资本', prop: 'registeredCapital', },
        // { label: '备注', prop: 'remark', },
        // { label: '所属集团', prop: 'affiliatedGroup', },
        // { label: '集团地址', prop: 'groupAddress', },
        // { label: '集团类型', prop: 'groupType', },
        // { label: '能源管理机构', prop: 'managementAgency', },
        // { label: '主管节能领导', prop: 'supervisor', },
        // { label: '主管节能领导电话', prop: 'supervisorPhone', },
        // { label: '能管负责人姓名', prop: 'principal', },
        // { label: '能管负责人电话', prop: 'principalPhone', },
        // { label: '能源管理师证号', prop: 'managerId', },
        // { label: '是否能源管理体系认证', prop: 'managementCertification', },
        // { label: '认证日期', prop: 'certificationDate', },
        // { label: '认证机构', prop: 'certificationBody', },
        // { label: '企业主要生产线名称', prop: 'mainProductionLine', },
        // { label: '企业主导产品', prop: 'leadingProduct', },
        // { label: '租户ID', prop: 'tenantId', },
        // { label: '创建时间', prop: 'creationTime', },
        // { label: '创建用户', prop: 'creationId', },
      ],
    }
  },
  computed: {
    themeColor () {
      return this.$store.state.user.theme
    },
  },
  mounted () {
    this.query()
  },
  methods: {
    reload () {
      this.query()
    },
    query () {
      this.loading = true
      return api.getListEnterprise().then(res => {
        res.data.forEach(v => {
          const { longitude, latitude } = v
          v.lnglat = (longitude && latitude) ? `${longitude},${latitude}` : ''
        })
        this.lists = res.data
      }).finally(() => {
        this.loading = false
      })
    },
    add () {
      this.modalTitle = '新增企业'
      this.modalData = {}
      this.$nextTick(() => {
        this.$refs.modal.open()
      })
    },
    edit (row) {
      this.modalTitle = '编辑企业'
      this.modalData = { ...row }
      this.$nextTick(() => {
        this.$refs.modal.open()
      })
    },
    del (row) {
      confirmDel(this).then(() => {
        api.del(row.id).then(res => {
          if (res.code === 200) {
            this.reload()
            this.$message.success('删除成功')
          }
        })
      }).catch(() => {
      })
    },
  },
}
</script>

<style scoped>

</style>
